
import tips from "../../../class/Utils/Tips"
Page({

  data: {
    luyinstate: '0',
    luyinplaytig: true,
    playRecord: 'playRecord',
    is_show: true,
    tempFilePathstig: 0,
    img_arr:[],
      uplimg:[],
      mp3_s:0,
      tongbu:0,
      btn_title:"加载中...",
      btn_dis:true,
      i:0,
      pic_post_data: [],
      content_pic_list:[
        { pic:'https://toocle.y.netsun.com/Uploads/mingpian_pic/5b97b690138c0.jpg'},
        { pic: 'http://img3.imgtn.bdimg.com/it/u=1597708766,1013398675&fm=26&gp=0.jpg' },
      ]
  },

  onLoad: function (options) {
    var that = this;
    //牛逼的扫描名片功能 begin
    if (options.pic_post_data == 1) {

      var pic_post_data = wx.getStorageSync('pic_post_data')
      that.setData({
        pic_post_data: pic_post_data
      })
    }
    //牛逼的扫描名片功能 end

      //裁切图片用 begin
      let { avatar } = options
      if (avatar) {
          that.setData({
              tempFilePathstig: 1,
              uplimg: avatar
          })
      }
      //裁切图片用 end

    that.recorderManager = wx.getRecorderManager();
    that.recorderManager.onError(function () {
      that.tip("录音失败！")
      that.luyinshibai("录音失败执行动作")
    });
    that.recorderManager.onStop(function (res) {
        console.log(res)
      that.setData({
        src: res.tempFilePath,
          mp3_s:1
      })
      that.luyinwancheng("录音停止执行动作")
      //console.log(res.tempFilePath)
      that.tip("录音完成！请点击 '保存名片信息' 进行录音保存")
    });

    that.innerAudioContext = wx.createInnerAudioContext();
    that.innerAudioContext.onEnded((res) => {
      that.setData({
        playRecord: 'playRecord'
      })
    })
    that.innerAudioContext.onError((res) => {
      that.tip("播放录音失败！")
    })
      /*
      判断用户 首次进入  录入 userid
       */
      var user = wx.getStorageSync('user');
      //console.log(user)
      var s = options.s||0;
      var id = options.id||0;
      if(s == '1'){
          that.setData({
              mp_id:id
          })
          // http.req("/Mingpian/panduan_qiye_mingpian",{userid:user.userid},function (resss) {
          //       if(resss.code == '1'){
          //           wx.showModal({
          //               title: '温馨提示',
          //               content: '系统检测到之前设置过名片信息,是否同步',
          //               success: function(res) {
          //                   if (res.confirm) {
          //                       http.req("/Mingpian/tongbu",{id:resss.id},function (infores) {
          //                           //console.log(infores)
          //                           that.setData({
          //                               info:infores,
          //                               tongbu:1
          //                           })
          //                           if(infores.qianmingmp3){
          //                               that.setData({
          //                                   luyinstate: '0',
          //                                   primarytig: 'primary',
          //                                   luyinplaytig: false,
          //                                   src:infores.qianmingmp3
          //                               })
          //                           }
          //                       })
          //                   } else if (res.cancel) {
          //                       //console.log('用户点击取消')
          //                   }
          //               }
          //           })
          //       }
          // })
      }else{
          // http.req("/Mingpian/panduan",{userid:user.userid},function (res) {
          //     //console.log(res)
          //     that.setData({
          //         mp_id:res
          //     })
          //     /*
          //      info
          //      */
          //     that.info()

          // })
      }




  },
    info:function () {
      var that = this
        var user = wx.getStorageSync('user');
    },
  onShow: function () {
     var that = this
     if (that.data.src == null){
       that.setData({
         primarytig: 'default',
         luyinplaytig: 'false'
       })
     }else{
       that.setData({
         primarytig: 'primary',
         luyinplaytig: 'true'
       })
     }
    var pic_post_data = wx.getStorageSync('pic_post_data')
    that.setData({
      pic_post_data: pic_post_data
    })
  },
  //录音失败
  luyinshibai:function(shibai){
    var that = this
    that.setData({
      luyinstate: '0',
    })
  },
  //录音停止
  luyinwancheng:function(){
    var that = this
    that.setData({
      luyinstate: '0',
      primarytig: 'primary',
      luyinplaytig: false,
    })
  },
  /**生命周期函数--监听页面隐藏
  onHide: function () {
    var that= this
    var src = that.data.src;
    that.innerAudioContext.src = that.data.src;
    that.innerAudioContext.stop()
    
  },**/
  /**提示**/
  tip: function (msg) {
    wx.showModal({
      title: '提示',
      content: msg,
      showCancel: false
    })
  },
  /**录制mp3音频**/
   startRecordMp3: function () {
    var that = this
    that.recorderManager.start({
      format: 'mp3',
      duration: 15000,
    });
    that.setData({
      luyinstate: '1',
      primarytig: 'default',
      luyinplaytig: true,
      playRecord: 'playRecord',

    })
  },
  /**停止录音**/
   stopRecord: function () {
    var that = this
    that.recorderManager.stop()
    that.setData({
      luyinstate: '0',
      primarytig: 'primary',
      luyinplaytig: false,
    })
    var src = that.data.src;
    //console.log(src)
  },
  /**播放录音**/
  playRecord: function () {
    var that = this;
    var src = that.data.src;
      //console.log(src)
    if (src == '') {
      that.tip("请先录音！")
      return;
    }
    that.innerAudioContext.src = src;
    that.innerAudioContext.play()
    that.setData({
      playRecord: 'playRecordstop'
    })
    //console.log('这是播放事件' + that.data.playRecord)

  },
  //
  playRecordstop: function() {
    var that = this;
    var src = that.data.src;
    that.innerAudioContext.src = that.data.src;
    that.innerAudioContext.stop()
    that.setData({
      playRecord: 'playRecord'
    })
    //console.log('这是停止事件' + that.data.playRecord)
  },
  //删除录音
  deleteRecord:function(){
    var that=this
    // that.innerAudioContext.destroy()
    var src = that.data.src;

    wx.showModal({
      title: '删除提示',
      content: '确认要删除已有语音签名吗？删除后要重新录制',
      success: function (res) {
        if (res.confirm) {
            http.req("/Mingpian/del_luyin",{id:that.data.mp_id})
          that.innerAudioContext.stop()
          that.setData({
            src: '',
            luyinstate: '0',
            primarytig: 'default',
            luyinplaytig: 'false',
              mp3_s:0
          })
        } else if (res.cancel) {

        }
      }
    })



  },
  //选择图片发送
  uploadimg: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 选择图片的数量，最多9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有

      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片


          const src = res.tempFilePaths[0]
          wx.redirectTo({
              url: `../upload/upload?src=${src}&id=${that.data.mp_id}`
          })



        // var tempFilePaths = res.tempFilePaths
        // that.setData({
        //   tempFilePathstig: 1,
        //   uplimg: tempFilePaths
        // })
        //   var data = {
        //       id: that.data.mp_id
        //   }
        //   var rootDocment = app.httpurl.url
        //   wx.uploadFile({
        //       url: rootDocment + '/Mingpian/edit_mingpian',
        //       filePath: that.data.uplimg[0],
        //       name: 'pic', //文件对应的参数名字(key)
        //       formData: data,  //其它的表单信息
        //       success: function (suc) {
        //           //console.log(suc)
        //           var data = JSON.parse(suc.data);
        //           if (data.status == '1') {
        //               console.log("头像上传成功");
        //               tips.ok("头像上传成功")
        //
        //           }
        //       },
        //   })
      }
    })
  },
  //上传照片展示图片
  upimg: function () {
    var that = this;
    var img_length = that.data.img_arr.length;
    var content_pic_length = that.data.content_pic_list.length
    wx.chooseImage({
      sizeType: ['compressed'],
      success: function (res) {
        var num = res.tempFilePaths.length + img_length + content_pic_length
        if (num > 6) {
          wx.showModal({
            title: '温馨提示',
            content: '最多上传6张图片',
            showCancel: false
          })
          return false
        }
        that.setData({
          img_arr: that.data.img_arr.concat(res.tempFilePaths),

        })
        if (that.data.info.qianmingmp3) {
          that.setData({
            luyinstate: '0',
            primarytig: 'primary',
            luyinplaytig: false,
          })
        }

      }
    })




  },
  //删除当前新上传的 照片展示
  xxx: function (e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var img_arr = that.data.img_arr;
    img_arr.splice(index, 1);
    this.setData({
      img_arr: img_arr
    });
  },

  //提交表单
  formSubmit: function (e) {

    var that = this;
    var user = wx.getStorageSync('user');
    that.setData({
      btn_title: "数据处理中。。",
      btn_dis: true
    })
    /**新加头像*/
    if (that.data.uplimg.length) {
      wx.uploadFile({
        url: app.httpurl.url + '/Api/change_touxiang',
        filePath: that.data.uplimg,
        name: 'pic', //文件对应的参数名字(key)
        formData: { uid: user.userid },  //其它的表单信息
        success: function (suc) {
          console.log('提交')
        },
      })
    } else {
      console.log('没提交')
    }
  },

    up: function (id,cb) {
        var that = this;
        var data = {
            pid: id
        }
        var i = that.data.i
        console.log(that.data.img_arr)
        console.log(i)
        var rootDocment = app.httpurl.url
        wx.uploadFile({
            url: rootDocment + '/Mingpian/content_img',
            filePath: that.data.img_arr[i],
            name: 'pic', //文件对应的参数名字(key)
            formData: data,  //其它的表单信息
            success: function (suc) {
            }, complete: function (complete) {
                console.log("处理多图第"+i+"张");
                if(complete.data== '1'){
                    i++
                    if (i < that.data.img_arr.length) {//若图片还没有传完，则继续调用函数
                        that.setData({
                            i:i
                        })
                        that.up(id,cb)
                    }else{
                        console.log("多图处理完毕");
                        return typeof cb == "function" && cb()
                    }
                }

            }
        })
    },
    /*删除已有图片*/
    content_pic_del:function (e) {
        var that = this
        var index = e.currentTarget.dataset.index
        wx.showModal({
            title: '温馨提示',
            content: '是否删除?',
            success: function(res) {
                if (res.confirm) {
                    var content_pic_list = that.data.content_pic_list;
                    content_pic_list.splice(index, 1);
                    that.setData({
                        content_pic_list: content_pic_list
                    });
                } else if (res.cancel) {
                    //console.log('用户点击取消')
                }
            }
        })
    },
})